package com.wzl.qs;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.wzl.qs.db.DbAdapter;
import com.wzl.qs.db.ENote;

public class mFavorite extends ListActivity {
	DbAdapter dbAdapter;
	private static int NUM_FAVORITES = 5;
	
	private static final int ABOUT = 1;
	private static final int HELP = 2;

	// private static final int ACTIVITY_CREATE = 0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favorite);

		dbAdapter = new DbAdapter(this);
		dbAdapter.open();

		// List the notes
		displayFavoriteList();
	}

	private void displayFavoriteList() {
		ArrayList<ENote> noteList = dbAdapter.GetFavoriteENotes(NUM_FAVORITES);
		FavoriteNoteAdapter noteAdapter = new FavoriteNoteAdapter(this,
				noteList);
		setListAdapter(noteAdapter);

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		ENote eNote = ((NoteAdapterView) v).getENote();

		Intent i = new Intent(this, NoteView.class);
		i.putExtra(DbAdapter.NID_FIELD, eNote.getNid());

		// Start activity to execute the intent
		startActivity(i);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem mi = menu.add(Menu.NONE, ABOUT, Menu.NONE, "About");
		mi.setIcon(R.drawable.qsm);
		MenuItem mihelp = menu.add(Menu.NONE, HELP, Menu.NONE, "Help");
		mihelp.setIcon(android.R.drawable.ic_dialog_info);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int itemid = item.getItemId();
		switch (itemid) {
		case ABOUT:
			Intent i = new Intent(this, About.class);
			startActivity(i);
			break;

		case HELP:
			Intent aboutIntent = new Intent(this, HelpFavorates.class);
			startActivity(aboutIntent);
			break;

		}
		return super.onOptionsItemSelected(item);
	}


	@Override
	protected void onResume() {
		super.onResume();
		displayFavoriteList();
	}
}
